<template>
  <div class="home" v-if="refresh" :key="refresh">
    <front-header></front-header>
    <div class="main-content">
      <!-- 加载动画 -->
      <div class="loading-container" v-if="loading">
        <div class="loading-spinner">
          <i class="el-icon-loading"></i>
        </div>
        <div class="loading-text">加载中...</div>
      </div>

      <!-- 轮播图和公告区域 -->
      <div v-else>
        <div class="carousel-notice-section">
          <div class="carousel-wrapper">
            <front-carousel></front-carousel>
          </div>
          <div class="notice-wrapper">
            <front-notice></front-notice>
          </div>
        </div>
        
     

        <!-- 新品上架专区 -->
        <div class="section new-products-section">
          <div class="section-header">
            <div class="title-wrapper">
              <h2 class="section-title">
                <i class="el-icon-shopping-bag-1"></i>
                <span>新品上架</span>
              </h2>
              <div class="title-line"></div>
              <div class="subtitle">尝鲜体验，新鲜好物抢先购</div>
            </div>
            <div class="more-btn" @click="$router.push('/products?sort=updatedAt,desc')">
              <span>查看更多</span>
              <i class="el-icon-arrow-right"></i>
            </div>
          </div>
          
          <!-- 加载状态 -->
          <div v-if="newProductsLoading" class="loading-container-small">
            <i class="el-icon-loading"></i>
            <span>新品加载中...</span>
          </div>
          
          <!-- 空状态 -->
          <div v-else-if="newProducts.length === 0" class="empty-state">
            <div class="empty-illustration">
              <i class="el-icon-shopping-bag-1"></i>
            </div>
            <h3>暂无新品</h3>
            <p>敬请期待更多新鲜好物</p>
          </div>
          
          <!-- 新品展示区 -->
          <div v-else class="new-products-container">
            <div class="new-product-card" v-for="product in newProducts" :key="product.id" @click="$router.push(`/product/${product.id}`)">
              <div class="product-image">
                <el-image 
                  :src="product.imageUrl?.startsWith('http') ? product.imageUrl : `/api${product.imageUrl}`" 
                  fit="cover"
                  lazy
                >
                  <div slot="error" class="image-placeholder">
                    <i class="el-icon-picture-outline"></i>
                  </div>
                </el-image>
              </div>
              <div class="product-info">
                <h3 class="product-name">{{ product.name }}</h3>
                <div class="product-meta">
                  <span class="product-category">{{ product.category?.name }}</span>
                  <span class="product-date">{{ formatDate(product.updatedAt) }}</span>
                </div>
                <div class="product-price">
                  <span class="current-price">¥{{ product.isDiscount == 1 ? product.discountPrice : product.price }}</span>
                  <span class="original-price" v-if="product.isDiscount == 1">¥{{ product.price }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 推荐商品区域 -->
        <div class="section">
          <div class="section-header">
            <div class="title-wrapper">
              <h2 class="section-title">
                <i class="el-icon-star-on"></i>
                <span>为您推荐</span>
              </h2>
              <div class="title-line"></div>
              <div class="subtitle">精选优质好物，专属于您的推荐</div>
            </div>
            <div class="more-btn" @click="$router.push('/products')">
              <span>查看更多</span>
              <i class="el-icon-arrow-right"></i>
            </div>
          </div>
          <!-- 空状态 -->
          <div class="empty-state" v-if="recommendProducts.length === 0">
            <div class="empty-illustration">
              <i class="el-icon-shopping-cart-2"></i>
            </div>
            <h3>暂无推荐商品</h3>
            <p>我们正在为您精选优质好物</p>
          </div>
          <!-- 商品列表 -->
          <el-row :gutter="24" v-else>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" v-for="product in recommendProducts" :key="product.id" class="product-col">
              <product-card :product="product" @add-to-cart="handleAddToCart" @toggle-favorite="handleToggleFavorite"></product-card>
            </el-col>
          </el-row>
        </div>

        <!-- 分类精选和销量排行区域 -->
        <div class="section">
          <div class="section-header">
            <div class="title-wrapper">
              <h2 class="section-title">
                <i class="el-icon-menu"></i>
                <span>分类精选</span>
              </h2>
              <div class="title-line"></div>
              <div class="subtitle">按分类浏览更多优质好物</div>
            </div>
            <div class="more-btn" @click="$router.push('/products')">
              <span>查看更多</span>
              <i class="el-icon-arrow-right"></i>
            </div>
          </div>
          
          <div class="main-content-row">
            <!-- 左侧分类和商品区域 -->
            <div class="category-products-container">
              <!-- 左侧分类列表 -->
              <div class="categories-sidebar">
                <div 
                  v-for="category in topCategories" 
                  :key="category.id"
                  class="category-list-item"
                  :class="{ active: selectedCategoryId === category.id }"
                  @click="selectCategory(category.id)"
                >
                  <span class="category-name">{{ category.name }}</span>
                  <span class="product-count">({{ category.productCount }})</span>
                </div>
              </div>
              
              <!-- 中间商品卡片 -->
              <div class="category-products-grid">
                <!-- 加载状态 -->
                <div v-if="categoryProductsLoading" class="loading-simple">
                  <i class="el-icon-loading"></i> 加载中...
                </div>
                
                <!-- 空状态 -->
                <div v-else-if="categoryProducts.length === 0" class="empty-simple">
                  <i class="el-icon-goods"></i>
                  <p>暂无商品</p>
                </div>
                
                <!-- 商品卡片网格 -->
                <div v-else class="products-grid">
                  <div 
                    v-for="product in categoryProducts" 
                    :key="product.id" 
                    class="product-mini-card"
                    @click="$router.push(`/product/${product.id}`)"
                  >
                    <div class="product-mini-image">
                      <el-image 
                        :src="product.imageUrl?.startsWith('http') ? product.imageUrl : `/api${product.imageUrl}`" 
                        fit="cover"
                        lazy
                      >
                        <div slot="error" class="image-placeholder">
                          <i class="el-icon-picture-outline"></i>
                        </div>
                      </el-image>
                      <div class="discount-tag" v-if="product.isDiscount == 1">特惠</div>
                    </div>
                    <div class="product-mini-info">
                      <div class="product-mini-name">{{ product.name }}</div>
                      <div class="product-mini-price">
                        <span class="current-price">¥{{ product.isDiscount == 1 ? product.discountPrice : product.price }}</span>
                        <span class="original-price" v-if="product.isDiscount == 1">¥{{ product.price }}</span>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>

            <!-- 右侧销量排行区域 -->
            <div class="sales-ranking-container">
              <div class="sales-ranking-header">
                <i class="el-icon-trophy"></i>
                <span>销量排行</span>
              </div>
              
              <!-- 加载状态 -->
              <div v-if="topSalesLoading" class="loading-simple">
                <i class="el-icon-loading"></i> 加载中...
              </div>
              
              <!-- 空状态 -->
              <div v-else-if="topSalesProducts.length === 0" class="empty-simple">
                <i class="el-icon-data-analysis"></i>
                <p>暂无销量数据</p>
              </div>
              
              <!-- 销量排行列表 -->
              <div v-else class="sales-ranking-list">
                <div 
                  v-for="(product, index) in topSalesProducts" 
                  :key="product.id" 
                  class="ranking-item"
                  @click="$router.push(`/product/${product.id}`)"
                >
                  <div class="ranking-number" :class="`rank-${index + 1}`">{{ index + 1 }}</div>
                  <div class="ranking-info">
                    <div class="ranking-name">{{ product.name }}</div>
                    <div class="ranking-meta">
                      <span class="ranking-price">¥{{ product.isDiscount == 1 ? product.discountPrice : product.price }}</span>
                      <span class="ranking-sales">已售 {{ product.salesCount }}</span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <front-footer></front-footer>
  </div>
</template>

<script>
import FrontHeader from '@/components/front/FrontHeader.vue'
import FrontFooter from '@/components/front/FrontFooter.vue'
import FrontCarousel from '@/components/front/FrontCarousel.vue'
import FrontCategory from '@/components/front/FrontCategory.vue'
import ProductCard from '@/components/front/ProductCard.vue'
import FrontNotice from '@/components/front/FrontNotice.vue'
import Request from '@/utils/request'

export default {
  name: 'Home',
  components: {
    FrontHeader,
    FrontFooter,
    FrontCarousel,
    FrontCategory,
    ProductCard,
    FrontNotice
  },
  data() {
    return {
      recommendProducts: [],
      isLoggedIn: false,
      refresh: true,
      loading: true,
      topCategories: [],
      selectedCategoryId: null,
      categoryProducts: [],
      categoryProductsLoading: true,
      topSalesLoading: true,
      topSalesProducts: [],
      newProducts: [],
      newProductsLoading: true
    }
  },
  computed: {
   
  },
  methods: {
    // 修改 checkLoginStatus 方法
    checkLoginStatus() {
      const token = localStorage.getItem('token');
      const userStr = localStorage.getItem('frontUser');
      
      if (token && userStr) {
        try {
          const user = JSON.parse(userStr);
       
          // 检查用户角色，如果不是 USER 则注销
          if (user.role !== 'USER') {
            this.logout();
            this.$message.warning('请使用普通用户账号访问');
            return;
          }
          this.isLoggedIn = true;
        } catch (error) {
          this.logout();
        }
      } else {
        this.isLoggedIn = false;
      }
    },

    // 修改 logout 方法
    logout() {
      localStorage.removeItem('token');
      localStorage.removeItem('frontUser');
      this.isLoggedIn = false;
      // 强制刷新整个页面
      window.location.reload()
   
    },

    // 获取推荐商品
    async getRecommendProducts() {
      try {
        this.loading = true; // 开始加载
        // 重新检查登录状态
        this.checkLoginStatus();
        
        let products = [];
        
        if (!this.isLoggedIn) {
          // 未登录状态 - 获取随机排序的前8条商品
          const res = await Request.get('/product/page?status=1&size=8');
          if (res.code === '0' && res.data) {
            products = res.data.records || [];
            // 随机排序
            products = products.sort(() => Math.random() - 0.5);
          }
        } else {
          // 登录状态 - 获取个性化推荐
          const userStr = localStorage.getItem('frontUser');
          if (!userStr) {
            throw new Error('User info not found');
          }
          const userId = JSON.parse(userStr).id;
          const res = await Request.get(`/recommend/user/${userId}`);
          if (res.code === '0' && res.data) {
            products = res.data.records || res.data || [];
          }
          
          // 获取收藏状态
          try {
            const favRes = await Request.get(`/favorite/user/${userId}`);
            if (favRes.code === '0' && favRes.data) {
              const favorites = favRes.data;
              products = products.map(product => ({
                ...product,
                isFavorite: favorites.some(f => f.productId === product.id && f.status === 1)
              }));
            }
          } catch (error) {
            console.error('获取收藏状态失败:', error);
            products = products.map(product => ({
              ...product,
              isFavorite: false
            }));
          }
        }

        // 设置推荐商品
        this.recommendProducts = products.map(product => ({
          ...product,
          isFavorite: product.isFavorite || false
        }));

      } catch (error) {
        console.error('获取推荐商品失败:', error);
        // 如果获取失败，设置空数组避免报错
        this.recommendProducts = [];
      } finally {
        // 结束加载
        setTimeout(() => {
          this.loading = false;
        }, 100);
      }
    },
    
    // 添加到购物车
    handleAddToCart(product) {
      this.$message({
        type: 'success',
        message: '已添加到购物车'
      })
    },
    // 切换收藏状态
    async handleToggleFavorite({ product, status }) {
      // 更新本地数据
      const targetProduct = this.recommendProducts.find(p => p.id === product.id)
      if (targetProduct) {
        targetProduct.isFavorite = status === 1
      }
      
      // 同时更新新品列表中的收藏状态
      const newProduct = this.categoryProducts.find(p => p.id === product.id)
      if (newProduct) {
        newProduct.isFavorite = status === 1
      }
    },
    async getTopCategories() {
      try {
        const res = await Request.get('/category/top');
        if (res.code === '0' && res.data) {
          this.topCategories = res.data;
          // 默认选中第一个分类
          if (this.topCategories.length > 0) {
            this.selectCategory(this.topCategories[0].id);
          }
        }
      } catch (error) {
        console.error('获取分类列表失败:', error);
      }
    },
    async getCategoryProducts() {
      try {
        this.categoryProductsLoading = true;
        if (!this.selectedCategoryId) return;
        
        const params = {
          categoryId: this.selectedCategoryId,
          status: 1,
          size: 6,
          sortField: 'updatedAt',
          sortOrder: 'desc'
        };
        
        const res = await Request.get('/product/page', { params });
        if (res.code === '0' && res.data) {
          this.categoryProducts = res.data.records || [];
        }
      } catch (error) {
        console.error('获取分类商品失败:', error);
        this.categoryProducts = [];
      } finally {
        this.categoryProductsLoading = false;
      }
    },
    selectCategory(categoryId) {
      this.selectedCategoryId = categoryId;
      this.getCategoryProducts();
    },
    async getTopSalesProducts() {
      try {
        this.topSalesLoading = true;
        const res = await Request.get('/product/topSales');
        if (res.code === '0' && res.data) {
          this.topSalesProducts = res.data.records || [];
        }
      } catch (error) {
        console.error('获取销量数据失败:', error);
        this.topSalesProducts = [];
      } finally {
        this.topSalesLoading = false;
      }
    },
    async getNewProducts() {
      try {
        this.newProductsLoading = true;
        const res = await Request.get('/product/new');
        if (res.code === '0' && res.data) {
          this.newProducts = res.data.records || [];
        }
      } catch (error) {
        console.error('获取新品失败:', error);
        this.newProducts = [];
      } finally {
        this.newProductsLoading = false;
      }
    },
    formatDate(date) {
      const options = { year: 'numeric', month: 'long', day: 'numeric' };
      return new Date(date).toLocaleDateString(undefined, options);
    }
  },
  created() {
    // 添加登录状态检查
    this.checkLoginStatus();
    this.getRecommendProducts();
    this.getTopCategories();
    this.getTopSalesProducts();
    this.getNewProducts();
  },
  mounted() {
    const userStr = localStorage.getItem('frontUser');
    if (userStr) {
      this.isLoggedIn = true;
    }
  }
}
</script>

<style scoped>
.home {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: linear-gradient(135deg, #fff8e1 0%, #fff3e0 100%);
  position: relative;
  overflow: hidden;
}

/* 背景装饰 */
.home::before, .home::after {
  content: '';
  position: absolute;
  border-radius: 50%;
  z-index: 0;
}

.home::before {
  width: 600px;
  height: 600px;
  top: -200px;
  right: -100px;
  background: linear-gradient(135deg, rgba(255, 152, 0, 0.1), rgba(255, 193, 7, 0.05));
}

.home::after {
  width: 400px;
  height: 400px;
  bottom: -100px;
  left: -100px;
  background: linear-gradient(135deg, rgba(255, 152, 0, 0.1), rgba(255, 193, 7, 0.05));
}

.main-content {
  flex: 1;
  padding: 20px;
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
  box-sizing: border-box;
  position: relative;
  z-index: 1;
}

.section {
  margin: 32px 0;
  position: relative;
  overflow: hidden;
  border-radius: 24px;
  box-shadow: none;
  background: rgba(255, 255, 255, 0.95);
  padding: 24px;
  transition: transform 0.3s ease;
  border: 1px solid rgba(255, 193, 7, 0.1);
}

.section:hover {
  transform: translateY(-5px);
  box-shadow: none;
}

.section::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 5px;
  background: linear-gradient(to right, #FF9800, #FFC107);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  position: relative;
  z-index: 1;
}

.title-wrapper {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 16px;
}

.section-title {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
  color: #F57C00;
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  gap: 12px;
  margin-right: 16px;
  letter-spacing: 1px;
}

.section-title i {
  color: #FF9800;
  font-size: 22px;
  background: linear-gradient(135deg, #FF9800, #FFC107);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.title-line {
  position: absolute;
  bottom: -4px;
  left: 0;
  width: 100%;
  height: 8px;
  background: rgba(255, 193, 7, 0.2);
  border-radius: 4px;
  z-index: 0;
}

.subtitle {
  font-size: 14px;
  color: #666;
  position: relative;
  top: 2px;
}

.more-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  border-radius: 20px;
  background: linear-gradient(135deg, #FF9800, #FFC107);
  color: white;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
  box-shadow: none;
}

.more-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.2), transparent);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.more-btn:hover {
  transform: translateY(-2px);
  box-shadow: none;
}

.more-btn:hover::before {
  opacity: 1;
}

.more-btn i {
  font-size: 14px;
  transition: transform 0.3s ease;
  position: relative;
  z-index: 1;
}

.more-btn span {
  position: relative;
  z-index: 1;
}

.more-btn:hover i {
  transform: translateX(3px);
}

.carousel-notice-section {
  display: flex;
  gap: 20px;
  margin: 20px 0;
  height: 380px;
  margin-bottom: 50px;
}

.carousel-wrapper {
  flex: 3; /* 调整为占据更多空间 */
  min-width: 0;
  height: 100%;
  border-radius: 24px;
  overflow: hidden;
  box-shadow: none;
  border: 1px solid rgba(255, 193, 7, 0.1);
}

.notice-wrapper {
  flex: 1; /* 调整为占据较少空间 */
  min-width: 300px;
  max-width: 400px;
  height: 100%;
  border-radius: 24px;
  overflow: hidden;
  box-shadow: none;
  border: 1px solid rgba(255, 193, 7, 0.1);
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 0;
  background: linear-gradient(to bottom, #ffffff, #fff8e1);
  border-radius: 16px;
  box-shadow: none;
  margin-bottom: 24px;
  position: relative;
  overflow: hidden;
  border: 1px solid rgba(255, 193, 7, 0.1);
}

.empty-illustration {
  position: relative;
  width: 120px;
  height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 20px;
  background: rgba(255, 193, 7, 0.1);
  border-radius: 50%;
}

.empty-state i {
  font-size: 64px;
  color: #FF9800;
  opacity: 0.8;
  z-index: 1;
}

.empty-state h3 {
  font-size: 20px;
  font-weight: 600;
  color: #F57C00;
  margin: 0 0 8px 0;
}

.empty-state p {
  font-size: 14px;
  color: #666;
  margin: 0;
}

.product-col {
  margin-bottom: 24px;
  display: flex;
}

.product-col > div {
  width: 100%;
  height: 100%;
}

/* 新品上架区域样式 */
.new-products-section {
  margin-bottom: 32px;
}

.new-products-container {
  display: flex;
  gap: 20px;
  overflow-x: auto;
  padding: 10px 0;
  scroll-behavior: smooth;
  scrollbar-width: thin;
}

.new-products-container::-webkit-scrollbar {
  height: 6px;
}

.new-products-container::-webkit-scrollbar-thumb {
  background-color: rgba(255, 152, 0, 0.3);
  border-radius: 10px;
}

.new-products-container::-webkit-scrollbar-track {
  background-color: rgba(255, 152, 0, 0.05);
  border-radius: 10px;
}

.new-product-card {
  flex: 0 0 auto;
  width: 200px;
  background: #fff;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: none;
  transition: all 0.3s;
  cursor: pointer;
  position: relative;
  border: 1px solid rgba(255, 193, 7, 0.1);
}

.new-product-card:hover {
  transform: translateY(-5px);
  box-shadow: none;
  border-color: rgba(255, 193, 7, 0.3);
}

.product-image {
  position: relative;
  width: 100%;
  padding-bottom: 100%;
  overflow: hidden;
}

.product-image .el-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transition: transform 0.5s;
}

.new-product-card:hover .product-image .el-image {
  transform: scale(1.08);
}

.image-placeholder {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: #909399;
  font-size: 24px;
}

.product-info {
  padding: 16px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.product-name {
  font-size: 15px;
  font-weight: 600;
  color: #F57C00;
  margin-bottom: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.product-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.product-category {
  font-size: 12px;
  color: #666;
  background: rgba(255, 193, 7, 0.1);
  padding: 2px 8px;
  border-radius: 10px;
}

.product-date {
  font-size: 12px;
  color: #666;
}

.product-price {
  margin-top: auto;
  display: flex;
  align-items: center;
  gap: 6px;
}

.current-price {
  font-size: 16px;
  font-weight: 600;
  color: #FF9800;
}

.original-price {
  font-size: 12px;
  text-decoration: line-through;
  color: #909399;
}

/* 分类精选和销量排行区域样式 */
.main-content-row {
  display: flex;
  gap: 24px;
}

.category-products-container {
  flex: 3;
  display: flex;
  gap: 24px;
  background: #fff;
  border-radius: 16px;
  box-shadow: none;
  padding: 20px;
  min-height: 300px;
  border: 1px solid rgba(255, 193, 7, 0.1);
}

.categories-sidebar {
  flex: 1;
  padding-right: 20px;
  border-right: 1px solid rgba(255, 193, 7, 0.2);
  display: flex;
  flex-direction: column;
}

.category-list-item {
  padding: 12px 16px;
  cursor: pointer;
  border-radius: 8px;
  margin-bottom: 10px;
  transition: all 0.3s;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border: 1px solid transparent;
}

.category-list-item:hover {
  background: rgba(255, 193, 7, 0.08);
  transform: translateX(5px);
  border-color: rgba(255, 193, 7, 0.2);
}

.category-list-item.active {
  background: linear-gradient(to right, rgba(255, 152, 0, 0.15), rgba(255, 193, 7, 0.05));
  color: #FF9800;
  font-weight: 500;
  border-left: 3px solid #FF9800;
}

.category-list-item .category-name {
  font-size: 15px;
  font-weight: 500;
  color: #333;
}

.category-list-item.active .category-name {
  color: #FF9800;
}

.category-list-item .product-count {
  font-size: 13px;
  color: #666;
  background: rgba(0, 0, 0, 0.05);
  padding: 2px 8px;
  border-radius: 10px;
}

.category-list-item.active .product-count {
  background: rgba(255, 152, 0, 0.15);
  color: #FF9800;
}

.category-products-grid {
  flex: 3;
  min-width: 0;
  overflow-y: auto;
  padding-left: 20px;
}

.products-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 16px;
}

.product-mini-card {
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: none;
  transition: all 0.3s;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  border: 1px solid rgba(255, 193, 7, 0.1);
}

.product-mini-card:hover {
  transform: translateY(-4px);
  box-shadow: none;
  border-color: rgba(255, 193, 7, 0.3);
}

.product-mini-image {
  position: relative;
  width: 100%;
  padding-bottom: 100%;
  overflow: hidden;
}

.product-mini-image .el-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transition: transform 0.5s;
}

.product-mini-card:hover .el-image {
  transform: scale(1.08);
}

.discount-tag {
  position: absolute;
  top: 8px;
  right: 8px;
  background: linear-gradient(135deg, #ff6b6b, #ff8e8e);
  color: #fff;
  font-size: 12px;
  padding: 3px 8px;
  border-radius: 4px;
  font-weight: 500;
  box-shadow: none;
}

.product-mini-info {
  padding: 12px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.product-mini-name {
  font-size: 14px;
  font-weight: 500;
  color: #333;
  margin-bottom: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.product-mini-price {
  margin-top: auto;
  display: flex;
  align-items: center;
  gap: 6px;
}

.product-mini-price .current-price {
  font-size: 16px;
  font-weight: 600;
  color: #FF9800;
}

.product-mini-price .original-price {
  font-size: 12px;
  text-decoration: line-through;
  color: #909399;
}

.sales-ranking-container {
  flex: 1;
  background: #fff;
  border-radius: 16px;
  box-shadow: none;
  padding: 20px;
  min-height: 300px;
  display: flex;
  flex-direction: column;
  border: 1px solid rgba(255, 193, 7, 0.1);
}

.sales-ranking-header {
  display: flex;
  align-items: center;
  padding-bottom: 16px;
  border-bottom: 1px solid rgba(255, 193, 7, 0.2);
  margin-bottom: 16px;
}

.sales-ranking-header i {
  font-size: 20px;
  color: #FF9800;
  margin-right: 8px;
  background: linear-gradient(135deg, #FF9800, #FFC107);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.sales-ranking-header span {
  font-size: 16px;
  font-weight: 600;
  color: #F57C00;
  letter-spacing: 1px;
}

.sales-ranking-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.ranking-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  cursor: pointer;
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
  transition: all 0.3s;
  border-radius: 8px;
}

.ranking-item:hover {
  background: rgba(255, 193, 7, 0.05);
  transform: translateX(5px);
}

.ranking-item:last-child {
  border-bottom: none;
}

.ranking-number {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  margin-right: 12px;
  font-size: 14px;
  background: #f5f5f5;
  color: #666;
  box-shadow: none;
}

.ranking-number.rank-1 {
  background: linear-gradient(135deg, #FFD700, #FFC107);
  color: #fff;
  box-shadow: none;
}

.ranking-number.rank-2 {
  background: linear-gradient(135deg, #C0C0C0, #E0E0E0);
  color: #fff;
  box-shadow: none;
}

.ranking-number.rank-3 {
  background: linear-gradient(135deg, #CD7F32, #D2691E);
  color: #fff;
  box-shadow: none;
}

.ranking-info {
  flex: 1;
  min-width: 0;
}

.ranking-name {
  font-size: 14px;
  font-weight: 500;
  color: #333;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 4px;
}

.ranking-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.ranking-price {
  font-size: 16px;
  font-weight: 600;
  color: #FF9800;
}

.ranking-sales {
  font-size: 12px;
  color: #666;
  background: rgba(255, 193, 7, 0.1);
  padding: 2px 8px;
  border-radius: 10px;
}

.loading-simple {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px;
  color: #666;
  font-size: 14px;
}

.loading-simple i {
  margin-right: 8px;
  font-size: 20px;
  color: #FF9800;
}

.empty-simple {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  color: #666;
}

.empty-simple i {
  font-size: 48px;
  color: #FF9800;
  opacity: 0.6;
  margin-bottom: 16px;
}

.empty-simple p {
  font-size: 14px;
  color: #666;
  margin: 0;
}

.loading-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 500px;
}

.loading-spinner {
  font-size: 48px;
  color: #FF9800;
  animation: pulse 1.5s infinite;
}

.loading-text {
  margin-top: 16px;
  font-size: 16px;
  color: #666;
  font-weight: 500;
}

@keyframes pulse {
  0% {
    opacity: 0.6;
    transform: scale(0.95);
  }
  50% {
    opacity: 1;
    transform: scale(1.05);
  }
  100% {
    opacity: 0.6;
    transform: scale(0.95);
  }
}

/* 响应式布局 */
@media (max-width: 992px) {
  .main-content-row {
    flex-direction: column;
  }

  .category-products-container, .sales-ranking-container {
    flex: none;
  }

  .sales-ranking-container {
    margin-top: 24px;
  }
}

@media (max-width: 768px) {
  .category-products-container {
    flex-direction: column;
    padding: 16px;
  }

  .sales-ranking-container {
    padding: 16px;
  }

  .categories-sidebar {
    flex: none;
    width: 100%;
    border-right: none;
    border-bottom: 1px solid rgba(255, 193, 7, 0.2);
    padding-right: 0;
    padding-bottom: 16px;
    margin-bottom: 16px;
    display: flex;
    flex-direction: row;
    overflow-x: auto;
    gap: 10px;
  }

  .category-list-item {
    flex: 0 0 auto;
    margin-bottom: 0;
    padding: 8px 16px;
    white-space: nowrap;
  }

  .category-list-item:hover {
    transform: none;
  }

  .category-products-grid {
    padding-left: 0;
    padding-top: 16px;
  }

  .products-grid {
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 12px;
  }

  .ranking-item {
    padding: 10px 12px;
  }

  .new-products-container {
    padding: 0 16px;
  }

  .new-product-card {
    width: 160px;
  }

  .carousel-notice-section {
    flex-direction: column;
    height: auto;
  }

  .carousel-wrapper, .notice-wrapper {
    width: 100%;
    max-width: none;
    height: 250px;
  }

  :deep(.el-row) {
    margin-left: -8px !important;
    margin-right: -8px !important;
  }

  :deep(.el-col) {
    padding-left: 8px !important;
    padding-right: 8px !important;
  }
}

/* 修复el-row和el-col的样式问题 */
:deep(.el-row) {
  margin-left: -12px !important;
  margin-right: -12px !important;
  display: flex;
  flex-wrap: wrap;
  width: calc(100% + 24px);
}

:deep(.el-col) {
  padding-left: 12px !important;
  padding-right: 12px !important;
  margin-bottom: 24px;
}

@media (max-width: 768px) {
  :deep(.el-row) {
    margin-left: -8px !important;
    margin-right: -8px !important;
    width: calc(100% + 16px);
  }

  :deep(.el-col) {
    padding-left: 8px !important;
    padding-right: 8px !important;
    margin-bottom: 16px;
  }
}
</style>